package com.game.battleship.util;

import java.util.ArrayList;
import java.util.List;

import com.game.battleship.data.Game;
import com.game.battleship.data.Ship;
import com.game.battleship.rule.CellForbiddingRule;

/**
 * A class with ship related utilities
 * @author Vasko and Tony
 * @editor vtkhanh
 *
 */
public class ShipUtil {
	/**
	 * converts an array of mShips into a an array of their fields
	 * @param mShips
	 * @return Description
	 */
	public static int[] getShipsCells(Ship[] ships) {
		List<Integer> resultList = new ArrayList<Integer>();
		Ship tempShip;
		for (short i = 0; i < ships.length; i++) {
			tempShip = ships[i];
			for (short j = 0; j < tempShip.getLength(); j++) {
				resultList.add(tempShip.getBoardCells()[j]);
			}
		}
		int[] result = new int[resultList.size()];
		for (short k = 0; k < result.length; k++) {
			result[k] = resultList.get(k);
		}
		return result;
	}
	
	public static short[] getFormationFromShips(Ship[] ships) {
		short[] result = new short[Game.BOARD_SIZE];
		for (int i = 0; i < result.length; i++) {
			result[i] = Game.WATER;
		}
		
		for (short shipIndex = 0; shipIndex < ships.length; shipIndex++) {
			Ship ship = ships[shipIndex];
			for (short i = 0; i < ship.getLength(); i++) {
				result[ship.getCell(i)] = (short)(shipIndex + 1); 
			}
		}
		return result;
	}
	/*public static short[] getShipsFromFormation(short[] formation) {
		
		return null;
	}*/
	/**
	 * Applying a forbidding rule get the cells that a ship forbids 
	 * @param ship
	 * @param rule
	 * @return Description
	 */
	public static List<Short> getForbiddenCellsFromShip(Ship ship, CellForbiddingRule rule) {
		List<Short> result = new ArrayList<Short>();
		int[] shipCells = ship.getBoardCells();
		
		for(int i : shipCells) {
			List<Integer> forbiddenCells = rule.getForbiddenCells(i); 
			for(int fc : forbiddenCells) {
				result.add((short) fc);
			}
		}

		return result;
	}
}
